package com.ihg.hiex.admin.web.controller.session;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.ihg.hiex.admin.entity.search.SessionReportSearch;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.account.DelegateService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.after.SessionReport;
import com.ihg.hiex.entity.user.Delegate;

@Controller
@MenuSetting("menu-sessions")
@RequestMapping("/session-reports")
public class SessionReportController {
    
    private static final Logger logger = LoggerFactory.getLogger(SessionReportController.class);
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private DelegateService delegateService;
    
    /**
     * 
     * @param login
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-session-report-list")
    public ModelAndView list(@Secure Login login, Page page, HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("session/report/session-report-list");
        
        int totals = sessionService.count(login);
        if (totals > 0) {

            List<Session> sessions = sessionService.list(login, page.with(totals));
            modelAndView.addObject("sessions", sessions);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "session/report/session-report-search";
    }

    /**
     * 
     * @param search
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-session-report-list")
    public ModelAndView onSearch(SessionReportSearch search, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("session/report/session-report-view");
        
        // list sessionReport first
        Map<String, Map<String, SessionReport>> sessionReports = sessionService.searchSessionReport(search);
        modelAndView.addObject("sessionReports",sessionReports);
        
        // if sessionId is not null get this session's name
        if(search.getSession() != null) {
            Session session = sessionService.load(search.getSession());
            modelAndView.addObject("sessionName", session.getName());
        }
        
        // if delegateId is not null get this delegate
        if(search.getDelegate() != null) {
            Delegate delegate = delegateService.load(Delegate.class, search.getDelegate());
            modelAndView.addObject("delegateName", delegate.getName());
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/delegates", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult delegates(@RequestParam("sessionId") Long sessionId) {

        HttpResult httpResult = new HttpResult();

        try {
            
            Session session = sessionService.load(sessionId);
            
            List<Delegate> delegates = sessionService.listDelegate(session);
        
            httpResult.success().message("success").data("delegates", delegates);

        } catch (Exception e) {
            httpResult.fail("500").message("fail");
            logger.error("#onClose() - close session fail, exception is [{}]", e);
        }

        return httpResult;
        
    }
    
}